package com.example.qihuibao.utils;

import android.os.Handler;
import android.view.KeyEvent;

/**
 * author : cwwei
 * e-mail : 494545699@qq.com
 * date   : 2022/03/10
 * desc   : 解析扫码枪输入
 */

public class ScannerKeyEventHelper {

    private final Handler mHandler;
    private boolean mCaps;
    private OnScanSuccessListener mOnScanSuccessListener;
    private StringBuffer mStringBufferResult;
    private Runnable mScanningFishedRunnable;


    public ScannerKeyEventHelper(OnScanSuccessListener onScanSuccessListener) {
        mOnScanSuccessListener = onScanSuccessListener;
        mStringBufferResult = new StringBuffer();
        mHandler = new Handler();
        mScanningFishedRunnable = new Runnable() {
            @Override
            public void run() {
                performScanSuccess();
            }
        };
    }

    //扫码解析
    public void analysisKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode(); //字母大小写判断
        checkLetterStatus(event);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            char aChar = getInputCode(event);
            if (aChar != 0) {
                mStringBufferResult.append(aChar);
            }
            if (keyCode == KeyEvent.KEYCODE_ENTER) { //若为回车键，直接返回
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.post(mScanningFishedRunnable);
            } else { //延迟post，若500ms内，有其他事件
                mHandler.removeCallbacks(mScanningFishedRunnable);
                mHandler.postDelayed(mScanningFishedRunnable, 500);
            }
        }
    }

    private void checkLetterStatus(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) { //按着shift键，表示大写
                mCaps = true;
            } else { //松开shift键，表示小写
                mCaps = false;
            }
        }
    }


    //获取扫描内容 6921734962300
    private char getInputCode(KeyEvent event) {
        int keyCode = event.getKeyCode();
        char aChar;
        if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) { //字母
            aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A);
        } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { //数字
            aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0);
        } else { //其他符号
            switch (keyCode) {
                case KeyEvent.KEYCODE_PERIOD:
                    aChar = '.';
                    break;
                case KeyEvent.KEYCODE_MINUS:
                    aChar = mCaps ? '_' : '-';
                    break;
                case KeyEvent.KEYCODE_SLASH:
                    aChar = '/';
                    break;
                case KeyEvent.KEYCODE_BACKSLASH:
                    aChar = mCaps ? '|' : '\\';
                    break;
                case KeyEvent.KEYCODE_LEFT_BRACKET:
                    aChar = mCaps ? '{' : '[';
                    break;
                case KeyEvent.KEYCODE_RIGHT_BRACKET:
                    aChar = mCaps ? '}' : ']';
                    break;
                case KeyEvent.KEYCODE_SEMICOLON:
                    aChar = mCaps ? ':' : ';';
                    break;
                case KeyEvent.KEYCODE_APOSTROPHE:
                    aChar = mCaps ? '"' : '\'';
                    break;
                case KeyEvent.KEYCODE_COMMA:
                    aChar = mCaps ? '<' : ',';
                    break;
                case KeyEvent.KEYCODE_STAR:
                    aChar = '*';
                    break;
                case KeyEvent.KEYCODE_POUND:
                    aChar = '＃';
                    break;
                case KeyEvent.KEYCODE_SPACE:
                    aChar = ' ';
                    break;
                case KeyEvent.KEYCODE_PLUS :
                    aChar = '+';
                    break;
                case KeyEvent.KEYCODE_EQUALS :
                    aChar = '=';
                    break;
//                case KeyEvent.KEYCODE_DPAD_DOWN:
//                    aChar = ' ';
//                    break;
                default:
                    aChar = KeyEvent.KEYCODE_UNKNOWN;
                    break;
            }
        }
        return aChar;
    }

    private void performScanSuccess() {
        String barcode = mStringBufferResult.toString();
        if (mOnScanSuccessListener != null) mOnScanSuccessListener.onScanSuccess(barcode);
        mStringBufferResult.setLength(0);
    }

    public void onDestroy(){
        mHandler.removeCallbacks(null);
        mOnScanSuccessListener = null;
    }


    /**
     * 解析键盘输入
     * @param event
     */
    public void analysisKeyboardEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (event.getAction() == KeyEvent.ACTION_UP) {
            String keyValue = getExternalKeyValue(keyCode);
            if (keyValue == null) {
                return;//去掉无关的按键值
            }
            if (mOnScanSuccessListener != null) {
                mOnScanSuccessListener.onKeyValue(keyValue);
            }
        }
    }

    /**
     * 结合了yx_rk3288 和 AIOT-3288SF 的键盘值
     * 有新增设备时，添加对应的值到case即可
     * @param keyCode
     * @return
     */
    private String getExternalKeyValue(int keyCode) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_0:
            case KeyEvent.KEYCODE_NUMPAD_0:
                return "0";
            case KeyEvent.KEYCODE_1:
            case KeyEvent.KEYCODE_NUMPAD_1:
                return "1";
            case KeyEvent.KEYCODE_2:
            case KeyEvent.KEYCODE_NUMPAD_2:
                return "2";
            case KeyEvent.KEYCODE_3:
            case KeyEvent.KEYCODE_NUMPAD_3:
                return "3";
            case KeyEvent.KEYCODE_4:
            case KeyEvent.KEYCODE_NUMPAD_4:
                return "4";
            case KeyEvent.KEYCODE_5:
            case KeyEvent.KEYCODE_NUMPAD_5:
                return "5";
            case KeyEvent.KEYCODE_6:
            case KeyEvent.KEYCODE_NUMPAD_6:
                return "6";
            case KeyEvent.KEYCODE_7:
            case KeyEvent.KEYCODE_NUMPAD_7:
                return "7";
            case KeyEvent.KEYCODE_8:
            case KeyEvent.KEYCODE_NUMPAD_8:
                return "8";
            case KeyEvent.KEYCODE_9:
            case KeyEvent.KEYCODE_NUMPAD_9:
                return "9";
            case KeyEvent.KEYCODE_PERIOD:
            case KeyEvent.KEYCODE_NUMPAD_DOT:
                return ".";
            case KeyEvent.KEYCODE_NUMPAD_ADD:
                return "+";
            case KeyEvent.KEYCODE_NUMPAD_SUBTRACT:
                return "-";
            case KeyEvent.KEYCODE_NUMPAD_MULTIPLY:
                return "×";
            case KeyEvent.KEYCODE_NUMPAD_DIVIDE:
                return "÷";
            case KeyEvent.KEYCODE_GRAVE:
            case KeyEvent.KEYCODE_S:
                return "search";
            case KeyEvent.KEYCODE_DPAD_UP:
                return "up";
            case KeyEvent.KEYCODE_DPAD_DOWN:
                return "down";
            case KeyEvent.KEYCODE_TAB:
            case KeyEvent.KEYCODE_F:
            case KeyEvent.KEYCODE_F2:
                return "setting";
            case KeyEvent.KEYCODE_ESCAPE:
            case KeyEvent.KEYCODE_D:
                return "cancel";
            case KeyEvent.KEYCODE_DEL:
            case KeyEvent.KEYCODE_SHIFT_LEFT:
                return "delete";
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_NUMPAD_ENTER:
                return "enter";
            case KeyEvent.KEYCODE_A:
            case KeyEvent.KEYCODE_F1:
                return "F1";
            case KeyEvent.KEYCODE_B:
                return "F2";
            case KeyEvent.KEYCODE_C:
                return "F3";
            default:
                return "";
        }
    }



    //usb设备结果解析回调
    public interface OnScanSuccessListener {
        void onScanSuccess(String data);//扫码枪输入
        void onKeyValue(String key);//键盘输入
    }

}
